<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field['options'].displayStyle"
  >
    <div class="scene-DRC-jcs-widget" :style="getStyle('area')">
      <div class="header-wrp">
        <div class="logo-wrp flex-row align-center">
          <img
            class="logo"
            v-if="logoUrl"
            referrerpolicy="no-referrer"
            :src="logoUrl"
          />
        </div>
      </div>
      <!--科室-->
      <div class="dept">
        <ScrollX>
          {{ showName || "\u00a0" }}
        </ScrollX>
      </div>
      <!-- 诊室
      <div class="room">
        <ScrollX>
          {{ getRoomDeptInfo[optionsInfo['roomNameKey']] || '\u00a0' }}
        </ScrollX>
      </div> -->
      <div class="project-wrp">
        <div class="project-title">
          <span>检查项目</span>
        </div>
        <div class="project-content">
          <div
            class="project-item"
            :style="getWriteTextStyle(item)"
            v-for="(item, index) in optionsInfo.content"
            :key="index"
          >
            <span class="project-item-label">0{{ index + 1 }}</span
            ><span class="project-item-content"
              ><ScrollX align="left">{{ item.text }}</ScrollX></span
            >
          </div>
        </div>
      </div>
      <div class="project-wrp" style="margin-top: 97px">
        <div class="project-title">
          <span>注意事项</span>
        </div>
        <div class="tip-content">
          <ScrollY>
            <div class="tip-content-main" v-html="optionsInfo.tipText"></div>
          </ScrollY>
        </div>
      </div>
      <div class="wx-wrp">
        <ScrollX>{{ optionsInfo.bottomText || "\u00a0" }}</ScrollX>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import { pxToVw } from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import { NoticeBar, Swipe, SwipeItem } from "vant";
import { buildParentApi } from "@/utils/designer/util";
import vueSeamlessScroll from "vue-seamless-scroll";
import ScrollX from "@c/public/scroll/scrollX.vue";
import ScrollY from "@c/public/scroll/scrollY.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import { v4 as uuid } from "uuid";

let buildParent = buildParentApi();
export default {
  name: "scene-DRC-nx-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    CompOperate,
    NoticeBar,
    Swipe,
    SwipeItem,
    ScrollY,
    ScrollX,
    vueSeamlessScroll,
  },

  props: {
    widget: Object,
    devType: {
      type: Boolean,
      default: false,
    },
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi(),
    },
  },

  data() {
    return {
      states: true,
      widgetKey: uuid(),
      currKey: uuid(),
      optionsInfo: Object(),
      header: require("@a/images/design/header.jpeg"),
      pause: require("@a/images/design/pause.png"),
      code1: require("@a/images/design/placeholder-code.png"),
    };
  },

  computed: {
    showName() {
      let deptInfo = this.getRoomDeptInfo;
      return deptInfo[this.optionsInfo.roomNameKey] || "\u00a0";
    },
    logoUrl() {
      if (this.optionsInfo.logoUrl) {
        return this.optionsInfo.logoUrl?.val?.url;
      } else {
        return "";
      }
    },

    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = self.optionsInfo || Array();
        let Obj = Object();
        switch (val) {
          case "area":
            Reflect.set(Obj, "height", pxToVw(info.areaHeight));
            Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
            return Obj;
          case "currentFontSize":
            Reflect.set(Obj, "fontSize", pxToVw(info.currentFontSize));
            return Obj;
          case "picture":
            Reflect.set(Obj, "height", pxToVw(info.imageHeight));
            Reflect.set(Obj, "width", pxToVw(info.imageWidth));
            Reflect.set(Obj, "borderRadius", pxToVw(info.imageRadius));
            return Obj;
          case "doctorName":
            Reflect.set(Obj, "fontWeight", info.doctorFontWeight || "normal");
            return Obj;
        }
      };
    },

    getWriteTextStyle() {
      return function (val) {
        return {
          "font-Size": pxToVw(val.fontSize),
          color: val.fontColor,
          "font-weight": val.fontWeight,
          "text-align": val.textAlign,
          "line-height": pxToVw(val.lineHeight),
          height: pxToVw(val.lineHeight),
        };
      };
    },

    getDeptHideClassName() {
      let { checkList, deptRoomPosition } = this.optionsInfo;
      if (checkList && checkList.length === 1 && checkList[0] === 3) {
        return "dept-room-hideDept";
      } else {
        if (deptRoomPosition) {
          return "dept-room-position";
        }
      }
    },

    // 获取全量医患信息
    getRoomDoctorInfo() {
      let devType = this.devType;
      let roomDoctorInfo = this.$store.getters["getRoomDoctorInfo"];
      let list = devType ? roomDoctorInfo.apiData : roomDoctorInfo.mockData;
      if (!!list?.length) {
        return list.slice(0, 1);
      } else {
        return [{}];
      }
    },

    // 获取科室信息
    getRoomDeptInfo() {
      let devType = this.devType;
      let roomDoctorInfo = this.$store.getters["getRoomDeptInfo"];
      console.log(roomDoctorInfo);
      if (devType) {
        return roomDoctorInfo.apiData;
      } else {
        return roomDoctorInfo.mockData;
      }
    },

    // 科室可能因为医生是否登录会有差异
    getRoomDiffInfo() {
      let list = this.getRoomDoctorInfo;
      let deptInfo = this.getRoomDeptInfo;
      if (list?.length) {
        return list[0].deptName || deptInfo?.deptName || "\u00a0";
      } else {
        return deptInfo?.deptName || "\u00a0";
      }
    },

    //截取字符串长度
    sliceStrInfo() {
      return function (str, number) {
        if (str?.length) {
          return str?.length > number ? str.slice(0, number) : str;
        } else {
          return "\u00a0";
        }
      };
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      switch (key) {
        case "roomDeptInfo":
          this.states = true;
          buildParent.executeParentMethod(
            "writeLog",
            JSON.stringify({
              sign: "模板日志-科室信息",
              key: key,
              data: data,
            })
          );
          this.$store.dispatch("setRoomDeptInfo", data);
          break;
        case "roomDoctorInfo":
          this.states = true;
          buildParent.executeParentMethod(
            "writeLog",
            JSON.stringify({
              sign: "模板日志-医生信息",
              key: key,
              data: data,
            })
          );
          this.$store.dispatch(
            "setRoomDoctorInfo",
            this.$lodash.cloneDeep(data)
          );
          break;
      }
    },

    // 滚动结束
    handleScrollEnd(e, mode) {
      if (mode == "curr") {
        this.currKey = uuid();
      } else {
        this.widgetKey = uuid();
      }
    },

    handleError(event) {
      const loadedImg = event.target;
      this.states = false;
    },
  },

  watch: {
    field: {
      handler: function (val) {
        const self = this;
        self.$nextTick(() => {
          self.widgetKey = uuid();
          let key = val.options.config;
          self.optionsInfo = val.options[key];
        });
      },
      deep: true,
      immediate: true,
    },
  },

  destroyed() {
    this.unregisterFromRefList();
  },
};
</script>

<style lang="scss" scoped>
.scene-DRC-jcs-widget {
  box-sizing: border-box;
  flex: inherit;
  flex-direction: column;
  justify-content: space-between;
  color: #333;
  font-size: 40px;
  background: transparent;
  background-image: url("~@/components/assets/images/design/jcs-bg.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  overflow: hidden;
  .header-wrp {
    height: 199px;
    padding-left: 25px;
    .logo-wrp {
      flex: 1;
      max-width: 690px;
      max-height: 195px;
      overflow: hidden;
      .logo {
        max-width: 100%;
        max-height: 100%;
      }
    }
  }
  //科室
  .dept {
    //width: 98%;
    height: 160px;
    background-color: rgba(2, 26, 33, 1);
    color: white;
    font-size: 88px;
  }

  // 诊室号/诊室
  .room {
    width: calc(94%);
    font-size: 160px;
    color: white;
    height: 190px;
    margin-top: 24px;
    padding-bottom: 10px;
    border-bottom: 2px solid white;
    box-sizing: border-box;
    margin-left: auto; /* 新增 */
    margin-right: auto; /* 新增 */
  }
  .project-wrp {
    position: relative;
    margin: 0 40px;
    margin-top: 50px;
    padding-top: 20px;
    box-sizing: border-box;
    .project-title {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      box-sizing: border-box;
      margin: auto;
      font-weight: 600;
      font-size: 60px;
      color: #203355;
      line-height: 84px;
      text-align: center;
      background-image: url("~@/components/assets/images/design/jcs-title.png");
      background-size: contain;
      background-repeat: no-repeat;
      background-position: top center;
      z-index: 2;
    }
    .project-content {
      box-sizing: border-box;
      position: relative;
      height: 550px;
      background: rgba(0, 5, 31, 0.5);
      border-radius: 20px;
      border: 10px solid #3cdcee;
      z-index: 1;
      box-sizing: border-box;
      padding: 90px 89px 20px 89px;
      display: flex;
      flex-direction: column;
      .project-item {
        flex: 1;
        display: flex;
        align-items: center;
        font-weight: 500;
        font-size: 38px;
        color: #ffffff;
      }
      .project-item-label {
        box-sizing: border-box;
        width: 76px;
        flex-shrink: 0;
        height: 67px;
        background: linear-gradient(180deg, #2bdef1 0%, #0bccea 100%);
        border-radius: 35px 6px 35px 6px;
        font-size: 38px;
        color: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-right: 30px;
        vertical-align: middle;
      }
      .project-item-content {
        flex: 1;
        overflow: hidden;
      }
    }
    .tip-content {
      box-sizing: border-box;
      position: relative;
      height: 609px;
      background: rgba(0, 5, 31, 0.5);
      border-radius: 20px;
      border: 10px solid #3cdcee;
      padding-top: 100px;
      z-index: 1;
      .tip-content-main {
        padding: 20px 40px 20px 40px;
        color: #ffffff;
        font-weight: bold;
        font-size: 38px;
        color: #ffffff;
        line-height: 70px;
        text-align: left;
      }
    }
  }
  .wx-wrp {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 126px;
    overflow: hidden;
    background: linear-gradient(90deg, #2cdff2 0%, #09cbea 100%);
    font-weight: 400;
    font-size: 56px;
    color: #ffffff;
    padding: 0 28px;
    display: flex;
    align-items: center;
  }
}
</style>

